package com.edu.app.service.impl.security;

import com.edu.app.bean.security.SysSecurity;
import com.edu.app.constant.StaticConstant;
import com.edu.app.mapper.security.SysSecurityMapper;
import com.edu.app.service.sys.security.SysSecurityService;
import com.edu.app.threadlocal.TenantContext;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.ContentHandler;
import java.util.HashMap;
import java.util.Map;

@Service
public class SysSecurityServiceImpl implements SysSecurityService {
    @Autowired
    private SysSecurityMapper sysSecurityMapper;

    public SysSecurity getSecurityByName(String keyname){
        SysSecurity sysSecurity = sysSecurityMapper.getSecurityByName(keyname);
        return sysSecurity;
    }

    public Map<String, String> getSMSParam(String name,
                                           String teacher,
                                           Integer points,
                                           String phoneNumber,
                                           String lessonType,
                                           String time)
    {
        String jsonContent = "{" +
                "\"name\":\"" +
                name +
                "\"," +
                "\"teacher\":\"" +
                teacher +
                "\"," +
                "\"points\":\"" +
                points +
                "\"," +
                "\"lessonType\":\"" +
                lessonType +
                "\"," +
                "\"time\":\"" +
                time +
                "\" }";

        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("phoneNumber", phoneNumber);
        paramMap.put("msgSign", StaticConstant.msgSign);
        paramMap.put("templateCode", StaticConstant.templatePoints);
        paramMap.put("jsonContent", jsonContent);

        return paramMap;
    }

    public Map<String, String> getVerifyCodeParam(String phoneNumber){
        Integer code  =    (int)((Math.random()*9+1)*100000);
        //替换大鱼短信中的模板的变量，计算6位随机码，这里有个坑，"code"名称和大于的模板管理中
        // "你的注册验证码是${code}，五分钟有效，请勿告诉其他人"中的变量
        //名称要一致
        String jsonContent = "{\"code\":\"" + code + "\"}";

        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("phoneNumber", phoneNumber);
        paramMap.put("msgSign", StaticConstant.msgSign);
        paramMap.put("templateCode", StaticConstant.templateCode);
        paramMap.put("jsonContent", jsonContent);
        //paramMap.put("code", code.toString());

        return paramMap;
    }

    @Override
    public String getHashCode(String code, String userPattern) {
        TenantContext.setNeedTenantId(Boolean.FALSE);
        SysSecurity sysSecurity = getSecurityByName("VerifyCodeKey");
        TenantContext.setNeedTenantId(Boolean.TRUE);

        String salt = sysSecurity.getKey_value();

        //对code进行hash
        SimpleHash obj = new SimpleHash(StaticConstant.algorithmMethod, code, ByteSource.Util.bytes(salt+userPattern), StaticConstant.hashIterations);

        return obj.toString();
    }

    @Override
    public Boolean validateVerifyCode(String code, String hashcode, String userPattern){
        Map<String, String> map = new HashMap<String, String>();

        return hashcode.equalsIgnoreCase(getHashCode(code, userPattern));
    }
}
